package com.bawei.mall.product.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.mall.product.domain.MallProductFootmarkInfo;
import com.bawei.mall.product.domain.MallProductInfoNew;
import com.bawei.mall.product.mapper.MallProductFootmarkInfoMapper;
import com.bawei.mall.product.mapper.MallProductInfoMapper;
import com.bawei.mall.product.service.IMallProductFootmarkInfoService;
import com.bawei.system.domain.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：韩丹阳
 * @Package：com.bawei.mall.product.service.impl
 * @Project：mall_cloud
 * @name：MallProductFootmarkInfoServiceImpl
 * @Date：2024/7/2 下午 05:27
 */
@Service
public class MallProductFootmarkInfoServiceImpl implements IMallProductFootmarkInfoService {

    @Autowired
    private MallProductFootmarkInfoMapper mallProductFootmarkInfoMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MallProductInfoMapper mallProductInfoMapper;

    /**
     * 创建浏览记录
     * @param mallProductFootmarkInfo
     */
    @Override
    public void create(MallProductFootmarkInfo mallProductFootmarkInfo) {
        User user = getInfo();
        mallProductFootmarkInfo.setMemberId(user.getId());
        mallProductFootmarkInfo.setMemberName(user.getUsername());
        mallProductFootmarkInfo.setMemberNickname(user.getNickname());

        MallProductInfoNew mallProductInfoNew = mallProductInfoMapper.selectMallProductInfoNewById(mallProductFootmarkInfo.getProductId());

        mallProductFootmarkInfo.setProductId(mallProductInfoNew.getId());
        mallProductFootmarkInfo.setProductName(mallProductInfoNew.getName());
        mallProductFootmarkInfo.setProductPic(mallProductInfoNew.getPic());
//        mallProductFootmarkInfo.setProductPrice(mallProductInfoNew.getPrice());
        mallProductFootmarkInfo.setProductSubTitle(mallProductInfoNew.getSubTitle());

        mallProductFootmarkInfoMapper.create(mallProductFootmarkInfo);

    }

    @Override
    public List<MallProductFootmarkInfo> list(Integer pageNum, Integer pageSize) {
        User user = getInfo();
        return mallProductFootmarkInfoMapper.list(pageNum,pageSize,user.getId());
    }

    /**
     * 清空足迹
     */
    @Override
    public void clear() {
        User user = getInfo();
        mallProductFootmarkInfoMapper.clear(user.getId());
    }


    /**
     * 获取用户信息
     */
    public User getInfo() {
        String token = redisTemplate.opsForValue().get("token");
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(cusUserJSONString, User.class);
        return user;
    }


}
